package models

import (
	"gorm.io/gorm"
	"network_srv/basic/config"
	"time"
)

type Video struct {
	Id        uint           `gorm:"column:id;type:bigint UNSIGNED;primaryKey;not null;" json:"id"`
	Title     string         `gorm:"column:title;type:varchar(50);comment:标题;not null;" json:"title"`                                    // 标题
	CoverUrl  string         `gorm:"column:cover_url;type:varchar(255);comment:封面;not null;" json:"cover_url"`                           // 封面
	VideoType string         `gorm:"column:video_type;type:varchar(30);comment:分类;not null;" json:"video_type"`                          // 分类
	Hot       uint           `gorm:"column:hot;type:int;comment:热度;not null;" json:"hot"`                                                // 热度
	Status    uint           `gorm:"column:status;type:tinyint UNSIGNED;comment:视频状态:1:正常，2:隐藏;not null;default:1;" json:"status"` // 视频状态:1:正常，2:隐藏
	CreatedAt time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (v *Video) TableName() string {
	return "video"
}
func (v *Video) VideoList(page, pageSize int) (VideoLists []*Video, err error) { //视频列表
	err = config.DB.Order("hot desc").Where("video_type like ?", "%"+v.VideoType+"%").Scopes(Paginate(page, pageSize)).Find(&VideoLists).Error
	return
}
func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB { //分页
	return func(db *gorm.DB) *gorm.DB {

		if page <= 0 {
			page = 1
		}

		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}

		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}
